<?php
 
// No direct access
 
defined('_JEXEC') or die('Restricted access'); ?>

<script>
	function articulosSeleccionados() {
		return document.getElementById('articulosSeleccionados').value.split(',');		
	}

	function seleccionarArticulo(articulo) {
		var as = articulosSeleccionados();
		as.push(articulo);
		document.getElementById('articulosSeleccionados').value = as.join();
//		alert(document.getElementById('articulosSeleccionados').value);
	}	

	function deseleccionarArticulo(articulo) {
		var as = articulosSeleccionados();
		var donde = as.indexOf(articulo);
		as.splice(donde, 1);

		document.getElementById('articulosSeleccionados').value = as.join();
//		alert(document.getElementById('articulosSeleccionados').value);
	}	

	function seccionEstaCompleta(seccion) {
		var cuantosHdn = document.getElementById(seccion + 'Selected');
		return cuantosHdn.value == 0;
	}

	function validate() {
		var estaOk = seccionEstaCompleta('grafica')
			&& seccionEstaCompleta('portales')
			&& seccionEstaCompleta('radios')
			&& seccionEstaCompleta('television')
			&& seccionEstaCompleta('agencias');
		btnSubmit.disabled = !estaOk;
	}

	function seleccionar(article, target) {
		var cuantosHdn = document.getElementById(target + 'Selected');
		if (cuantosHdn.value == 0) { 
			alert("Ya se seleccionaron todas las notas permitidas para esta seccion. Puede excluir alguna y volver a intentarlo.");
			return false;
		}
		seleccionarArticulo(article);
		cuantosHdn.value--;
		var div = document.getElementById('article'+article);
		var targetDiv = document.getElementById(target + 'Div');
		var btnIncluir = document.getElementById('incluirButton'+article);
		var btnExcluir = document.getElementById('excluirButton'+article);

		// habilito excluir y deshabilito incluir
		btnExcluir.style.display = 'block';
		btnIncluir.style.display = 'none';

		// agrego la copia en el otro div
		targetDiv.appendChild(div.cloneNode(true));

		// escondo el original
		div.style.display = 'none';
		validate();
	}

	function deseleccionar(me, article, target) {
		// elimino la copia del div target
		me.parentNode.parentNode.removeChild(me.parentNode);
		// 		
		var cuantosHdn = document.getElementById(target + 'Selected');
		cuantosHdn.value++;
		deseleccionarArticulo(article);

		var div = document.getElementById('article'+article);
		var targetDiv = document.getElementById(target + 'Div');
		var btnIncluir = document.getElementById('incluirButton'+article);
		var btnExcluir = document.getElementById('excluirButton'+article);

		// habilito excluir y deshabilito incluir
		btnIncluir.style.display = 'block';
		btnExcluir.style.display = 'none';

		// muestro el original de nuevo :)
		div.style.display = 'block';
		
		// deshabilito el submit, si elimino implica que ya no son los que corresponden
		var btnSubmit = document.getElementById('btnSubmit');
		btnSubmit.disabled = true;
	}

</script>
<h1><?php echo JText::_( 'Generar Newsletter' ); ?></h1>
<form action="<?php echo $this->action ?>" method="post" name="adminForm" >

<fieldset>
<table class="adminform" style="width: 400px;" >
	<tr>
		<td class="key" valign="top" colspan="2">
			<label for="grafica">
				<?php echo JText::_( 'Gráfica (seleccione 3)' ); ?>:
			</label>
		</td>
	</tr>
	<tr>
		<td>
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox">
			<?php foreach($this->grafica as $article) { ?>
			  	<div id="article<?php echo $article->id; ?>" >
					<a target="_blank" href="<? echo JRoute::_(ContentHelperRoute::getArticleRoute($article->slug, $article->catslug, $article->sectionid));?>">
					<? echo $article->title;  ?>
					</a><br/>
					<p>Fuente: <?echo (!empty($article->Medio) ? "{$article->Medio}, " : '') . $article->category;?></p>
					<input id="incluirButton<?php echo $article->id; ?>" type="button" value="Incluir" onclick="javascript: seleccionar('<?php echo $article->id; ?>', 'grafica')">
					<input id="excluirButton<?php echo $article->id; ?>" type="button" value="Excluir" onclick="javascript: deseleccionar(this, '<?php echo $article->id; ?>', 'grafica')" style="display:none;">
					<hr/>
				</div>
		  	<? } ?>
		  	</div>
		</td>
		<td>
			<input type="hidden" id="graficaSelected" value="3" />
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox" id="graficaDiv">
				
			</div>
		</td>
	</tr>
	<tr>
		<td colspan="2" >
			&nbsp;
		</td>
	</tr>
	<tr>
		<td class="key" valign="top" colspan="2">
			<label for="portales">
				<?php echo JText::_( 'Portales de Internet (seleccione 3)' ); ?>:
			</label>
		</td>
	</tr>
	<tr>
		<td>
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox">
			<?php foreach($this->portales as $article) { ?>
			  	<div id="article<?php echo $article->id; ?>" >
					<a target="_blank" href="<? echo JRoute::_(ContentHelperRoute::getArticleRoute($article->slug, $article->catslug, $article->sectionid));?>">
					<? echo $article->title;  ?>
					</a><br/>
					<p>Fuente: <?echo (!empty($article->Medio) ? "{$article->Medio}, " : '') . $article->category;?></p>
					<input id="incluirButton<?php echo $article->id; ?>" type="button" value="Incluir" onclick="javascript: seleccionar('<?php echo $article->id; ?>', 'portales')">
					<input id="excluirButton<?php echo $article->id; ?>" type="button" value="Excluir" onclick="javascript: deseleccionar(this, '<?php echo $article->id; ?>', 'portales')" style="display:none;">
					<hr/>
				</div>
		  	<? } ?>
		  	</div>
		</td>
		<td>
			<input type="hidden" id="portalesSelected" value="3" />
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox" id="portalesDiv">
			</div>
		</td>
	</tr>
	<tr>
		<td colspan="2" >
			&nbsp;
		</td>
	</tr>
	<tr>
		<td class="key" valign="top" colspan="2">
			<label for="radios">
				<?php echo JText::_( 'Radio (seleccione 1)' ); ?>:
			</label>
		</td>
	</tr>
	<tr>
		<td>
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox">
			<?php foreach($this->radios as $article) { ?>
			  	<div id="article<?php echo $article->id; ?>" >
					<a target="_blank" href="<? echo JRoute::_(ContentHelperRoute::getArticleRoute($article->slug, $article->catslug, $article->sectionid));?>">
					<? echo $article->title;  ?>
					</a><br/>
					<p>Fuente: <?echo (!empty($article->Medio) ? "{$article->Medio}, " : '') . $article->category;?></p>
					<input id="incluirButton<?php echo $article->id; ?>" type="button" value="Incluir" onclick="javascript: seleccionar('<?php echo $article->id; ?>', 'radios')">
					<input id="excluirButton<?php echo $article->id; ?>" type="button" value="Excluir" onclick="javascript: deseleccionar(this, '<?php echo $article->id; ?>', 'radios')" style="display:none;">
					<hr/>
				</div>
		  	<? } ?>
		  	</div>
		</td>
		<td>
			<input type="hidden" id="radiosSelected" value="1" />
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox" id="radiosDiv">
			</div>
		</td>
	</tr>
	<tr>
		<td colspan="2" >
			&nbsp;
		</td>
	</tr>
	<tr>
		<td class="key" valign="top" colspan="2">
			<label for="television">
				<?php echo JText::_( 'Televisión (seleccione 1)' ); ?>:
			</label>
		</td>
	</tr>
	<tr>
		<td>
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox">
			<?php foreach($this->television as $article) { ?>
			  	<div id="article<?php echo $article->id; ?>" >
					<a target="_blank" href="<? echo JRoute::_(ContentHelperRoute::getArticleRoute($article->slug, $article->catslug, $article->sectionid));?>">
					<? echo $article->title;  ?>
					</a><br/>
					<p>Fuente: <?echo (!empty($article->Medio) ? "{$article->Medio}, " : '') . $article->category;?></p>
					<input id="incluirButton<?php echo $article->id; ?>" type="button" value="Incluir" onclick="javascript: seleccionar('<?php echo $article->id; ?>', 'television')">
					<input id="excluirButton<?php echo $article->id; ?>" type="button" value="Excluir" onclick="javascript: deseleccionar(this, '<?php echo $article->id; ?>', 'television')" style="display:none;">
					<hr/>
				</div>
		  	<? } ?>
		  	</div>
		</td>
		<td>
			<input type="hidden" id="televisionSelected" value="1" />
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox" id="televisionDiv">
			</div>
		</td>
	</tr>
	<tr>
		<td colspan="2" >
			&nbsp;
		</td>
	</tr>
	<tr>
		<td class="key" valign="top" colspan="2">
			<label for="agencias">
				<?php echo JText::_( 'Agencias de Noticias (seleccione 2)' ); ?>:
			</label>
		</td>
	</tr>
	<tr>
		<td>
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox">
			<?php foreach($this->agencias as $article) { ?>
			  	<div id="article<?php echo $article->id; ?>" >
					<a target="_blank" href="<? echo JRoute::_(ContentHelperRoute::getArticleRoute($article->slug, $article->catslug, $article->sectionid));?>">
					<? echo $article->title;  ?>
					</a><br/>
					<p>Fuente: <?echo (!empty($article->Medio) ? "{$article->Medio}, " : '') . $article->category;?></p>
					<input id="incluirButton<?php echo $article->id; ?>" type="button" value="Incluir" onclick="javascript: seleccionar('<?php echo $article->id; ?>', 'agencias')">
					<input id="excluirButton<?php echo $article->id; ?>" type="button" value="Excluir" onclick="javascript: deseleccionar(this, '<?php echo $article->id; ?>', 'agencias')" style="display:none;">
					<hr/>
				</div>
		  	<? } ?>
		  	</div>
		</td>
		<td>
			<input type="hidden" id="agenciasSelected" value="2" />
			<div style="width: 200px; height: 300px; overflow: auto;" class="inputbox" id="agenciasDiv">
			</div>
		</td>
	</tr>
	<tr>
		<td colspan="2" >
			&nbsp;
		</td>
	</tr>
	<tr>
		<td colspan="2" align="right">
			<input type="submit" value="Generar" id="btnSubmit"/>
		</td>
	</tr>
</table>
<input type="hidden" name="articulosSeleccionados" id="articulosSeleccionados" value="" />
<input type="hidden" name="task" value="generar" />
</form>